package logic

import (
	"context"
	"fmt"
	"net/http"
	"user-profile/api/internal/svc"
	"user-profile/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UploadLogic) Upload(req *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	file, m, err := req.FormFile("image")
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	fmt.Println(file, m, err)
	//ext := filepath.Ext(m.Filename)
	//if ext != ".png" && ext != ".jpeg" {
	//	response := types.Response{
	//		Code:    500,
	//		Message: "上传文件格式必须是.png,.jpeg",
	//		Data:    nil,
	//	}
	//}
	//if m.Size > 8*1024*1024 {
	//	response := types.Response{
	//		Code:    500,
	//		Message: "文件大小不能超过8M",
	//		Data:    nil,
	//	}
	//}
	//
	//fileName := time.Now(time.Parse("20060102150405")) + ext
	//yun, err := pkg.QiNiuYun(fileName, file)
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	if err != nil {
		return &types.Response{
			Code:    200,
			Message: "图片上传成功",
			//Data:    yun,
		}, nil

	}

	return
}
